package gttp.nspa.client.adm;

import gttp.nspa.client.adm.rpc.IDictServiceAsync;
import gttp.nspa.shared.om.Employee;
import gttp.nspa.shared.om.IEntity;
import gttp.nspa.shared.om.Locker;
import gttp.nspa.shared.om.Massa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public final class Dict<T extends IEntity> {

    public static final Dict<Locker>   LOCKER;

    public static final Dict<Massa>    MASSA;

    public static final Dict<Employee> THERAPIST;

    public static final Dict<Employee> CASHIER;

    static {
        IDictServiceAsync dictService = GWT.create(IDictServiceAsync.class);

        final Map lockers = new HashMap();
        dictService.listLocker(new AsyncCallback<List<Locker>>() {

            public void onSuccess(List<Locker> result) {
                for (Locker locker : result) {
                    lockers.put(locker.getCode(), locker);
                }
            }

            public void onFailure(Throwable caught) {
            }
        });
        LOCKER = new Dict(lockers);

        final Map massas = new HashMap();
        dictService.listMassa(new AsyncCallback<List<Massa>>() {

            public void onSuccess(List<Massa> result) {
                for (Massa massa : result) {
                    massas.put(massa.getCode(), massa);
                }
            }

            public void onFailure(Throwable caught) {
            }
        });
        MASSA = new Dict(massas);

        final Map therapists = new HashMap();
        dictService.listTherapist(new AsyncCallback<List<Employee>>() {

            public void onSuccess(List<Employee> result) {
                for (Employee therapist : result) {
                    therapists.put(therapist.getCode(), therapist);
                }
            }

            public void onFailure(Throwable caught) {
            }
        });
        THERAPIST = new Dict(therapists);

        final Map cashiers = new HashMap();
        dictService.listCashier(new AsyncCallback<List<Employee>>() {

            public void onSuccess(List<Employee> result) {
                for (Employee cashier : result) {
                    cashiers.put(cashier.getCode(), cashier);
                }
            }

            public void onFailure(Throwable caught) {
            }
        });
        CASHIER = new Dict(cashiers);
    }

    private Map<String, T>             entities;

    private Dict(Map entities) {
        this.entities = entities;
    }

    public T get(String code) {
        return entities.get(code);
    }

    public Collection<T> list() {
        return entities.values();
    }

    public T setOccupied(String code, boolean flag) {
        T ent = entities.get(code);
        if (ent instanceof Locker) {
            ((Locker) ent).setOccupied(flag);
        } else if (ent instanceof Employee) {
            ((Employee) ent).setOccupied(flag);
        }
        return ent;
    }

    public List<T> listActive(boolean flag) {
        List<T> list = new ArrayList<>();
        for (T ent : entities.values()) {
            if (ent.isActive() == flag)
                list.add(ent);
        }
        return list;
    }

    public List<T> listOccupied(boolean flag) {
        List<T> list = new ArrayList<>();
        for (T ent : entities.values()) {
            if (ent.isOccupied() == flag)
                list.add(ent);
        }
        return list;
    }
}
